home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / PrintSequence.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.0 KB  |  35 lines

  1. //: C21:PrintSequence.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Prints the contents of any sequence
  7. #ifndef PRINTSEQUENCE_H
  8. #define PRINTSEQUENCE_H
  9. #include <iostream>
  10.  
  11. template<typename InputIter>
  12. void print(InputIter first, InputIter last,
  13.   char* nm = "", char* sep = "\n", 
  14.   std::ostream& os = std::cout) { 
  15.   if(*nm != '\0') // Only if you provide a string
  16.     os << nm << ": " << sep; // is this printed
  17.   while(first != last)
  18.     os << *first++ << sep;
  19.   os << std::endl;
  20. }
  21.  
  22. // Use template-templates to allow type deduction
  23. // of the typename T:
  24. template<typename T, template<typename> class C>
  25. void print(C<T>& c, char* nm = "", 
  26.   char* sep = "\n", 
  27.   std::ostream& os = std::cout) {
  28.   if(*nm != '\0') // Only if you provide a string
  29.     os << nm << ": " << sep; // is this printed
  30.   std::copy(c.begin(), c.end(), 
  31.     std::ostream_iterator<T>(os, " "));
  32.   cout << endl;
  33. }
  34. #endif // PRINTSEQUENCE_H ///:~
  35.